﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class TransportEquipmentBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ITransportEquipmentDA TransportEquipmentDA = DataAccess.CreateTransportEquipmentDA();

        public TransportEquipmentBLL()
        {
            this.pubParasInfo = null;
        }

        public TransportEquipmentBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public TransportEquipmentBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<TransportEquipmentInfo> transportEquipmentInfos = new List<TransportEquipmentInfo>();
                TransportEquipmentInfo transportEquipmentInfo = null;
                foreach (string id in IDS)
                {
                    transportEquipmentInfo = TransportEquipmentDA.GetTransportEquipmentByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    transportEquipmentInfos.Add(transportEquipmentInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (TransportEquipmentDA.DeleteTransportEquipment(dataBase, tran, IDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<TransportEquipmentInfo>(dataBase, tran, transportEquipmentInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "TRANSPORT_EQUIPMENT", this.dataBaseID);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return TransportEquipmentDA.GetAllTransportEquipment(this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetInfoByID(string id)
        {
            return TransportEquipmentDA.GetTransportEquipmentByID(id, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            TransportEquipmentQueryEntity transportEquipmentQuery = obj as TransportEquipmentQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(transportEquipmentQuery.TransportEquipmentID))
            {
                sqlWhere.Add(" TRANSPORT_EQUIPMENT_ID " + transportEquipmentQuery.TransportEquipmentID);
            }
            if (!string.IsNullOrEmpty(transportEquipmentQuery.CarrierID))
            {
                sqlWhere.Add(" CARRIER_ID " + transportEquipmentQuery.CarrierID);
            }
            if (!string.IsNullOrEmpty(transportEquipmentQuery.EquipmentTypeID))
            {
                sqlWhere.Add(" EQUIPMENT_TYPE_ID " + transportEquipmentQuery.EquipmentTypeID);
            }
            if (!string.IsNullOrEmpty(transportEquipmentQuery.Status))
            {
                sqlWhere.Add(" STATUS " + transportEquipmentQuery.Status);
            }
            if (!string.IsNullOrEmpty(transportEquipmentQuery.Remark))
            {
                sqlWhere.Add(" REMARK " + transportEquipmentQuery.Remark);
            }
            if (!string.IsNullOrEmpty(transportEquipmentQuery.IsActive))
            {
                sqlWhere.Add(" TE.IS_ACTIVE " + transportEquipmentQuery.IsActive);
            }
            return TransportEquipmentDA.GetTransportEquipmentByQueryList(sqlWhere, transportEquipmentQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            string transportEquipmentID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                TransportEquipmentInfo transportEquipmentInfo = obj as TransportEquipmentInfo;
                if (TransportEquipmentDA.CheckTransportEquipmentIDUnique(dataBase, tran, transportEquipmentInfo.TransportEquipmentID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2520_001", this.pubParasInfo.UserLanguage, new List<string> { transportEquipmentInfo.TransportEquipmentID });
                }
                ICarrierDA carrierDA = DataAccess.CreateCarrierDA();
                if (!carrierDA.CheckCarrierIDUnique(dataBase, tran, transportEquipmentInfo.CarrierID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2520_002", this.pubParasInfo.UserLanguage);
                }
                else if (carrierDA.CheckAuthorizationCarrierUserID(dataBase, tran, transportEquipmentInfo.CarrierID, this.dataBaseID[0], this.pubParasInfo))
                {
                    PublicMethod.GetInstance().ShowAlertMessage(" F2140_002", this.pubParasInfo.UserLanguage, new List<string> { transportEquipmentInfo.CarrierID });
                }
                transportEquipmentInfo.Status = TransportEquipmentStatus.FREE;
                transportEquipmentInfo.UpdatedBy = transportEquipmentInfo.CreatedBy;
                transportEquipmentInfo.CreatedDate = DateTime.Now;
                transportEquipmentInfo.UpdatedDate = DateTime.Now;
                if (TransportEquipmentDA.InsertTransportEquipment(dataBase, tran, transportEquipmentInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<TransportEquipmentInfo>(dataBase, tran, null, transportEquipmentInfo, AuditAction.Insert, this.pubParasInfo.UserID, "TRANSPORT_EQUIPMENT", this.dataBaseID[0]);
                    transportEquipmentID = transportEquipmentInfo.TransportEquipmentID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return transportEquipmentID;
        }

        public string Update(object obj)
        {
            string transportEquipmentID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                TransportEquipmentInfo transportEquipmentInfo = obj as TransportEquipmentInfo;
                transportEquipmentInfo.UpdatedDate = DateTime.Now;
                TransportEquipmentInfo oldTransportEquipmentInfo = TransportEquipmentDA.GetTransportEquipmentByID(dataBase, tran, transportEquipmentInfo.TransportEquipmentID, this.dataBaseID[0], this.pubParasInfo);
                if (TransportEquipmentDA.UpdateTransportEquipment(dataBase, tran, transportEquipmentInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<TransportEquipmentInfo>(dataBase, tran, oldTransportEquipmentInfo, transportEquipmentInfo, AuditAction.Update, this.pubParasInfo.UserID, "TRANSPORT_EQUIPMENT", this.dataBaseID);
                    transportEquipmentID = transportEquipmentInfo.TransportEquipmentID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return transportEquipmentID;
        }
    }
}

